package com.aptemo.webetek.client.widgets.dialogs;

import com.aptemo.webetek.client.events.DecisionEvent;
import com.aptemo.webetek.client.events.DecisionHandler;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class InvitationDialog
		extends DialogBox implements HasDecisionHandlers
{

	private static InvitationDialogUiBinder	uiBinder	= GWT.create(InvitationDialogUiBinder.class);

	interface InvitationDialogUiBinder extends UiBinder<Widget, InvitationDialog>
	{
	}

	@UiField
	Label		infoField;

	@UiField
	TextBox		recipientEmailField;

	@UiField
	TextArea	invitationMessageField;

	@UiField
	Button		submitButton;

	@UiField
	Button		cancelButton;

	public InvitationDialog(String title)
	{
		this.setText(title);
		this.add(uiBinder.createAndBindUi(this));
	}

	public HasText getEmailField()
	{
		return recipientEmailField;
	}

	public HasText getMessageField()
	{
		return invitationMessageField;
	}

	@UiHandler("submitButton")
	void onSubmitClick(ClickEvent event)
	{
		// check email field
		if (recipientEmailField.getText().isEmpty())
		{
			//
			infoField.setText("Enter correct <email>");
			return;
		}
		infoField.setText("");
		hide();

		// fire event...
		DecisionEvent dev = new DecisionEvent(infoField);
		fireEvent(dev);

	}

	@UiHandler("cancelButton")
	void onCancelClick(ClickEvent event)
	{
		hide();
	}

	@Override
	public HandlerRegistration addDecisionHandler(DecisionHandler handler)
	{
		return addHandler(handler, DecisionEvent.TYPE);
	}
	
}
